import {sandBox, config, is} from '../../../lib/myapp.js'
Page({


    data: {
        rechargeValue: '',
        largessValue: '',
        select_member: null,
        mobile: '',
        message: '',
        code: {
            codeText: '获取验证码',
            access_token: '',
            total: 60
        },
        number: '',
        sending: false,
        info: ''
    },

    onShow(){
        var selectMember = wx.getStorageSync('select_member')
        this.setData({
            select_member: selectMember
        })

        this.getMobile()
    },

    changeValue(e){
        var key = e.currentTarget.dataset.recharge

        this.setData({
            [`${key}`]: !isNaN(e.detail.value) ? e.detail.value : '',

        })

        this.setData({
            total: Number(this.data.rechargeValue) + Number(this.data.largessValue)
        })
    },

    setNumber(e){

        this.setData({
            number: !isNaN(e.detail.value) ? e.detail.value : ''
        })

    },


    setInfo(e){
        this.setData({
            info: e.detail.value
        })
    },


    selectMember(){
        var url = getCurrentPages()[getCurrentPages().length - 1].route
        wx.navigateTo({
            url: `/pages/member/list/list?url=${url}`
        })
    },

    getMobile(){
        wx.showLoading();
        var oauth = wx.getStorageSync('user_token');
        sandBox.get({
            api: `api/O2O/balance/clerk_owner/mobile`,
            header: {Authorization: oauth}
        }).then(res => {
            wx.hideLoading();
            res = res.data;
            if (res.code === 200) {
                this.setData({
                    mobile: res.data.mobile
                })
            } else {
                wx.showModal({
                    content: '获取店长手机号码失败',
	                showCancel: false
                })
            }


        }).catch(rej => {
            wx.showModal({
                content: '获取店长手机号码失败',
	            showCancel: false
            })
            wx.hideLoading();
        })
    },
    getCode(){
        if (this.data.sending) return;
        var randoms = this.random();
        this.setData({
            sending: true,
            'code.codeText': "短信发送中",
            'code.access_token': randoms
        });
        var fn;
        fn = this.getLoginCode;
        fn(() => {
            var total = this.data.code.total;
            this.setData({
                'code.codeText': total + "秒后再发送"
            });
            var timer = setInterval(() => {
                total--;
                this.setData({
                    'code.codeText': total + "秒后再发送"
                });
                if (total < 1) {
                    this.setData({
                        sending: false,
                        'code.codeText': "获取验证码"
                    });
                    clearInterval(timer);
                }
            }, 1000);
        }, () => {
            wx.showModal({
                content:'获取验证码失败',
                showCancel:false
            })
            this.setData({
                sending: false,
                'code.codeText': "获取验证码"
            });
        });
    },
    random(){
        return Math.random().toString(36).substr(2, 24);
    },

    getLoginCode(resolve, reject){
        var message = null;
        if (!is.has(this.data.mobile)) {
            message = "手机号为空";
        } else if (!is.mobile(this.data.mobile)) {
            message = '手机号格式不正确';
        }
        if (message) {
            this.setData({
                message: message
            });
            reject();
            setTimeout(() => {
                this.setData({
                    message: ""
                });
            }, 3000)
            return
        }
        sandBox.post({
            api: "api/sms/verify-code",

            data: {
                mobile: this.data.mobile,
                access_token: this.data.code.access_token
            },
        }).then(res => {
            if (res.data.success) {
                resolve();
            }
            else {
                reject();
            }
        })
        // resolve();
    },

    confirm (){

        if (!this.data.mobile) {
            wx.showModal({
                content: '店长手机号码获取失败',
	            showCancel: false
            })

            return false
        }

        if (!this.data.rechargeValue && !this.data.largessValue) {
            wx.showModal({
                content: '充值金额和赠送金额不可同时为空',
	            showCancel: false
            })
            return false;
        }

        if (isNaN(this.data.total)) {
            wx.showModal({
                content: '请输入金额且金额为数字',
	            showCancel: false
            })

            return false;
        }

        if (!this.data.select_member) {
            wx.showModal({
                content: '会员不能为空',
	            showCancel: false
            })

            return false;
        }

        if (!this.data.number) {
            wx.showModal({
                content: '授权码不能为空',
	            showCancel: false
            })

            return false;
        }


        var user_id = this.data.select_member.user_bind.user_id
        var oauth = wx.getStorageSync('user_token')
        sandBox.post({
            api: `api/O2O/balance/charge/${user_id}`,
            header: {Authorization: oauth},
            data: {
                give: Number(this.data.largessValue),
                pay_amount: Number(this.data.rechargeValue),
                channel:'wx_pub_qr',
                mobile:this.data.mobile,
                code :this.data.number,
                access_token: this.data.code.access_token
            }
        }).then(res =>{
            res = res.data;
            if (res.code === 200 && res.data && res.data.order_no) {
                wx.redirectTo({
                    url:`/pages/store/payment/payment?order_no=${res.data.metadata.order_sn}&wx_pub_qr=${res.data.credential.wx_pub_qr}`
                });
	            wx.removeStorageSync('select_member');
            } else {
                wx.showModal({
                    content:'提交失败',
	                showCancel: false
                })
            }

        }).catch(()=>{


            wx.showModal({
                content:'服务器错误',
	            showCancel: false
            })
        })

    }
})